// pages/collect/collect.js
//获取app实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectList: null, //收藏列表
    isNone: false
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    const userId = app.globalData.userId;
    wx.request({
      url: app.baseURL + 'Mine&a=collect&user_id=' + userId,
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            collectList: res.data.result,
            isNone: !res.data.result.length
          });
          wx.hideLoading();
        }  
      }
    })
  },

  //删除收藏商品
  deleteCollect(e){
    const index = e.currentTarget.dataset.index;
    const cid = e.currentTarget.dataset.cid;
    const userId = app.globalData.userId;
    console.log(userId, cid)
    wx.showModal({
      title: '提示',
      content: '确定移出我的收藏吗',
      confirmColor: "#f00",
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: app.baseURL + 'Mine&a=del_collect',
            data: {
              user_id: userId,
              collect_id: cid
            },
            success: (res) => { 
              console.log(res);
              if (res.data.status == 1) {
                let data = this.data.collectList;
                data.splice(index, 1);
                this.setData({
                  collectList: data,
                  isNone: !data.length
                });
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                }) 
              }
               
            } 
            
          })
        }
      }
    })
  },

  //跳转到详情页面
  toDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detail/detail?item_id=' + id,
    })
  }
})